/**
 * Copyright 2002 Cordys R&D B.V. 
 * 
 * This file is part of the Cordys .NET Connector. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 /**
* @author Phani Kavuri
*/
package com.eibus.applicationconnector.ejb;

import java.lang.reflect.Field;
import com.eibus.xml.nom.Document;
import com.eibus.xml.nom.Node;

public class Marshaller {
    private Object objectClass;
    private Class clsObject;
    private static Document document = new Document();
    private int parentNode;

    public Marshaller(Object object)     {
        this.clsObject = object.getClass();
        this.objectClass = object;
        parentNode = document.createElement( clsObject.getName());
    }

    private Marshaller(Object object, String fldName)     {
        this.clsObject = object.getClass();
        this.objectClass = object;
        parentNode = document.createElement(fldName);
    }

    public int marshal() throws MarshallerException {
        Field[] fieldList = null;
        try {
            fieldList = clsObject.getFields();
        }
        catch(SecurityException ex) {
            throw new MarshallerException(ex.getMessage());
        }
        for(int i=0;i<fieldList.length;i++) {
            try {
                String fldName = fieldList[i].getName();
                Class fieldType = fieldList[i].getType();
                Object fldValue = fieldList[i].get(objectClass);
                if (!fieldType.isPrimitive() && !fieldType.isInstance(new String())) {
                    Marshaller obj = new Marshaller(fldValue, fldName);
                    int node = obj.marshal();
                    Node.appendToChildren(node, parentNode);
                }
                else {
                    int childNode = Node.setDataElement(parentNode, fldName, fldValue.toString());
                    Node.appendToChildren(childNode, parentNode);
                }
            }
            catch(SecurityException ex) {
                throw new MarshallerException(ex.getMessage());
            }
            catch(IllegalAccessException ex) {
                throw new MarshallerException(ex.getMessage());
            }
            catch(IllegalArgumentException ex) {
                throw new MarshallerException(ex.getMessage());
            }
        }
        return parentNode;
    }
/*   public static void main(String[] args)
    {
        int node = 0;
        City c = new City("Hyderabad","500033",40);
        Address a = new Address("Vanenburg IT Park", "HiTec City", "Software Units Layout",c);
        Address a1 = new Address("Plot No. 73", "Vemana Colony", "Chanda Nagar",c);
        Department d = new Department("Cordys WCP", "Q1-26", a);
        Employee e1 = new Employee("Phani","241",3,1000.00,a,a1,d);
        try
        {
            Marshaller ref = new Marshaller(e1);
            node = ref.marshal();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        System.out.println(Node.writeToString(node,true));

        Employee e = null;
        try {
            UnMarshaller o = new UnMarshaller(node);
            e = (Employee) o.unMarshal();
        }
        catch(UnMarshallerException ex) {
            ex.printStackTrace();
        }
        System.out.println(e.dept.address.city.pinCode);
    }
 */
}
